package com.czk.bootlettuce.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/8/8 19:48
 */
@RestController
@Slf4j
public class LoginController {
    /**
     * 主图redis
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {


        // 从redis拿到值
        if (redisTemplate.hasKey(username)) {
            log.info("从redis中取");
            Object o = redisTemplate.opsForValue().get(username);
            String str = (String) o;
            if (str.equals(password)) {
                // 清楚redis中错误记录
                redisTemplate.delete(username + "_ERROR");
                return "登陆成功";
            } else {
                Boolean aBoolean = redisTemplate.hasKey(username + "_ERROR");
                if (aBoolean) {
                    Integer i = (Integer) redisTemplate.opsForValue().get(username + "_ERROR");
                    if (i == 4) {
                        // 此时锁定账户，不能登录
                        return "账户被锁定，不能登陆";
                    }
                    // 设置超时时间
                    redisTemplate.opsForValue().set(username + "_ERROR", ++i);
                    // 重新设置超时时间
                    redisTemplate.expire(username + "_ERROR", 60, TimeUnit.SECONDS);
                    return "登录失败，一分钟之内在登错" + (5 - i) + "次账户奖杯锁定";
                } else {
                    int i = 1;
                    redisTemplate.opsForValue().set(username + "_ERROR", i);
                    redisTemplate.expire(username + "_ERROR", 60, TimeUnit.SECONDS);
                    return "登录失败，一分钟之内在登错" + (5 - i) + "次账户奖杯锁定";
                }
            }
        }
        return "登录失败,没有该用户名";
    }

    @PostMapping("login/time")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        @RequestParam("valCode") String valCode) {
        /**
         * 比对验证码
         */
        if (!Objects.equals(valCode,"123")) {
            return "验证码错误";
        }
        int Max=5;
        // 判断用户是否被限制
        Map<String, Object> map = this.loginUserLock(username);
        boolean b = (boolean) map.get("flag");
        if (b) {
            long time = (long) map.get("time");
            return "账户被锁定，剩余时间" + time + "分钟";
        }
        // 执行登录
        if (!Objects.equals("czk",password)){
            // 密码错误，查询是否由登录错误key
            if (redisTemplate.hasKey(username+":loginError")){
                Object o = redisTemplate.opsForValue().get(username + ":loginError");
                int i=(int)o;
                i++;
                if (i>=Max){
                    // 锁定账户
                    redisTemplate.opsForValue().set(username+":loginLock","lock",1,TimeUnit.HOURS);
                    return "账户已被锁定，清一个小时候再尝试";
                }
                redisTemplate.opsForValue().set(username+":loginError",i,60,TimeUnit.SECONDS);
                return "登陆失败你还可以登"+(Max-i)+"次";
            }else {
                redisTemplate.opsForValue().set(username+":loginError",1,60,TimeUnit.SECONDS);
                return "登陆失败，还可在等4次";
            }
        }else {
            // 登陆成功
            if (redisTemplate.hasKey(username+":loginError")){
                redisTemplate.delete(username+":loginError");
            }
            return "登录成功";
        }
    }

    public Map<String, Object> loginUserLock(String username) {
        Boolean aBoolean = redisTemplate.hasKey(username+":loginLock");
        Map<String, Object> map = new HashMap<>();
        if (aBoolean) {
            // 存在
            // h获取锁定时间
            Long expire = redisTemplate.getExpire(username+":loginLock", TimeUnit.MINUTES);
            map.put("flag", true);
            map.put("time", expire);
        } else {
            map.put("flag", false);
        }
        return map;
    }
}
